package com.example.mydy;

import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.adapter.DownlineIncomeAdapter;
import com.example.mydy.model.DownlineIncomeRecord;
import com.example.mydy.model.UserInfo;
import com.example.mydy.utils.MockDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 下级收益详情页面 - 展示下级贡献的收益明细
 */
public class DownlineIncomeDetailActivity extends AppCompatActivity {
    private static final String TAG = "DownlineIncomeDetail";

    private Button btnBack;
    private TextView tvTotalIncome, tvDownlineCount;
    private RadioGroup rgFilter;
    private RadioButton rbAll, rbLevel1, rbLevel2, rbLevel3;
    private RecyclerView rvDownlineIncome;
    private DownlineIncomeAdapter adapter;
    private List<DownlineIncomeRecord> downlineRecords = new ArrayList<>();
    private List<DownlineIncomeRecord> filteredRecords = new ArrayList<>();

    private MockDataManager mockDataManager;
    private String currentUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downline_income_detail);

        mockDataManager = MockDataManager.getInstance();
        currentUsername = mockDataManager.getCurrentUser().getUsername();

        initViews();
        setupClickListeners();
        loadDownlineData();
    }

    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        tvTotalIncome = findViewById(R.id.tv_total_income);
        tvDownlineCount = findViewById(R.id.tv_downline_count);
        rgFilter = findViewById(R.id.rg_filter);
        rbAll = findViewById(R.id.rb_all);
        rbLevel1 = findViewById(R.id.rb_level1);
        rbLevel2 = findViewById(R.id.rb_level2);
        rbLevel3 = findViewById(R.id.rb_level3);
        rvDownlineIncome = findViewById(R.id.rv_downline_income);

        // 设置RecyclerView
        rvDownlineIncome.setLayoutManager(new LinearLayoutManager(this));
        adapter = new DownlineIncomeAdapter(this, filteredRecords);
        rvDownlineIncome.setAdapter(adapter);
    }

    private void setupClickListeners() {
        btnBack.setOnClickListener(v -> finish());

        // 设置筛选按钮监听
        rgFilter.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_all) {
                filterRecords(0);
            } else if (checkedId == R.id.rb_level1) {
                filterRecords(1);
            } else if (checkedId == R.id.rb_level2) {
                filterRecords(2);
            } else if (checkedId == R.id.rb_level3) {
                filterRecords(3);
            }
        });
    }

    private void loadDownlineData() {
        // 获取下级用户列表
        List<UserInfo> downlineUsers = mockDataManager.getDownlineUsers(currentUsername);
        
        // 获取下级收益记录
        downlineRecords = mockDataManager.getUserDownlineIncomeRecords(currentUsername);
        
        // 计算统计数据
        double totalIncome = calculateTotalIncome(downlineRecords);
        
        // 更新UI显示
        tvTotalIncome.setText(String.format("¥ %.2f", totalIncome));
        tvDownlineCount.setText(String.valueOf(downlineUsers.size()));
        
        // 默认显示所有记录
        filterRecords(0);
    }

    /**
     * 按级别筛选记录
     * @param level 级别(0全部, 1一级, 2二级, 3三级)
     */
    private void filterRecords(int level) {
        filteredRecords.clear();
        
        if (level == 0) {
            filteredRecords.addAll(downlineRecords);
        } else {
            for (DownlineIncomeRecord record : downlineRecords) {
                if (record.getLevel() == level) {
                    filteredRecords.add(record);
                }
            }
        }
        
        adapter.notifyDataSetChanged();
    }

    /**
     * 计算总收益
     * @param records 记录列表
     * @return 总收益
     */
    private double calculateTotalIncome(List<DownlineIncomeRecord> records) {
        double total = 0;
        for (DownlineIncomeRecord record : records) {
            total += record.getAmount();
        }
        return total;
    }
} 